package com.vanysoft.business.data.controller;


import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.data.entity.Plant;
import com.vanysoft.business.data.entity.PlantUser;
import com.vanysoft.business.data.service.PlantService;
import com.vanysoft.business.data.service.PlantUserService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.common.utils.Excel.EasyPoiUtil;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.aop.VanySoftValidationException;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工厂信息表 前端控制器
 * </p>
 *
 * @author YangLei
 * @since 2018-07-01
 */
@Controller
@RequestMapping("/data/plant")
public class PlantController extends BaseController {

    @Autowired
    PlantService plantService;

    @Autowired
    PlantUserService plantUserService;


    @RequestMapping("index")
    public String index(HttpServletRequest request) {
        return "data/plant";
    }

    /**
     * 保存工厂信息
     *
     * @param plant
     * @param result
     * @param request
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse save(@Valid Plant plant, BindingResult result, HttpServletRequest request, @SessionUser UserInfo userInfo) {
        Plant entity = plantService.selectOne(new EntityWrapper<Plant>().eq("plant_code", plant.getPlantCode()).eq("is_enable", "1"));
        if (entity != null) {
            throw new VanySoftValidationException(getMessage("plant.hint.exist"));
        }
        plant.setCreateTime(new Date());
        plant.setCreateUser(userInfo.getId());
        plantService.insert(plant);
        return new JSONResponse();
    }


    /**
     * 更新工厂信息
     *
     * @param plant
     * @param result
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse update(@Valid Plant plant, BindingResult result, @SessionUser UserInfo userInfo) {
        plant.setModifyTime(new Date());
        plant.setModifyUser(userInfo.getId());
        plantService.updateById(plant);
        return new JSONResponse();
    }

    /**
     * 分页显示工厂列表
     *
     * @param page
     * @param key
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public JSONResponse list(Page<Plant> page, String key) {
        page = plantService.selectPage(page, key);
        return new JSONResponse(page);
    }

    @RequestMapping("export")
    public void export(Page<Plant> page, String key) {
        page = plantService.selectPage(page, key);
//        ExportParams params = new ExportParams("", "Plant Data", ExcelType.XSSF);
//        params.setFreezeCol(1);
//        map.put(NormalExcelConstants.DATA_LIST, page.getRecords());
//        map.put(NormalExcelConstants.CLASS, Plant.class);
//        map.put(NormalExcelConstants.PARAMS, params);
        EasyPoiUtil.exportExcel(page.getRecords(),"","Plant Data",Plant.class,"Plant List.xlsx",getResponse());
    }


    /**
     * 删除工厂
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public JSONResponse delete(String id, HttpServletRequest request) {
        Plant plant = plantService.selectById(id);
        PlantUser plantUser = plantUserService.selectOne(new EntityWrapper<PlantUser>().eq("plant_code", plant.getPlantCode()).eq("is_enable", "1"));
        if (plantUser != null) {
            throw new VanySoftValidationException(getMessage("plant.hint.delete"));
        }
        //TODO 当该工厂与供应商存在关联关系
        plant.setIsEnable("0");
        plantService.updateById(plant);
        return new JSONResponse();
    }

    /**
     * 所有工厂
     *
     * @return
     */
    @RequestMapping("all")
    @ResponseBody
    public JSONResponse queryAll() {
        List<Plant> plants = plantService.selectList(new EntityWrapper<Plant>().eq("is_enable", "1"));
        return new JSONResponse(plants);
    }

}

